//
//  LikeUserViewController.m
//  Molian
//
//  Created by 胡津 on 4/6/2015.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "LikeUserViewController.h"
#import "MeCell.h"
#import "MLUserDataViewController.h"
#import "UserCell.h"
// 每次拉取的用户数目
#define LoadDataCount 30
@interface LikeUserViewController ()<UITableViewDelegate,UITableViewDataSource>
//视图列表
@property (nonatomic, strong)NSMutableArray *likeUsers;
@property (nonatomic, strong)Utils *uview;

@end

@implementation LikeUserViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.∂
    self.title = @"喜欢的人";
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    self.tableView.frame = CGRectMake(0, 0, DeviceWidth, DeviceHeight);
    self.likeUsers = [NSMutableArray array];
     self.uview = [[Utils alloc]init];
    [_uview viewControllerPrompt:self.view.bounds Text:@"还没有喜欢的人" Image:[UIImage imageNamed:@"xinlianren_nul"]];
    [self.view addSubview:_uview.viewPrompt];
    if ([self.likeUsers count] == 0) {
        _uview.viewPrompt.hidden = NO;
    }else{
        _uview.viewPrompt.hidden = YES;
    }
    [self loadLikeUserData];
    
    
}
//上拉
- (void)upPullLoadData
{
    if(self.likeUsers.count<LoadDataCount/2){
        [self.tableView footerEndRefreshing];
        return;
    }
    [self loadLikeUserData];
}
//下拉
- (void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{
    //加载数据
    self.currentPage = 0;
    [self loadLikeUserData];
}
- (void)loadLikeUserData
{
    AVQuery *likeUserQuery = [MLUserLike query];
    [likeUserQuery includeKey:@"likeUser"];
    [likeUserQuery whereKey:@"user" equalTo:[MLUser currentUser]];
    likeUserQuery.limit = LoadDataCount;
    likeUserQuery.skip = self.currentPage *LoadDataCount;
    likeUserQuery.cachePolicy = kAVCachePolicyNetworkElseCache;
    likeUserQuery.maxCacheAge = 7200;
    [likeUserQuery orderByDescending:@"createdAt"];
    [HUD setOnViewWithTitle:@"加载中" animated:YES];
   
    [likeUserQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [self.tableView footerEndRefreshing];
        [self.slimeView endRefresh];
        [HUD hideFromViewAnimated:YES];
        if (!error) {
            if (self.currentPage == 0) {
                [self.likeUsers removeAllObjects];
            }
            for (MLUserLike *obj in objects) {
                [self.likeUsers addObject:obj];
            }
            if ([self.likeUsers count] == 0) {
                _uview.viewPrompt.hidden = NO;
            }else{
                _uview.viewPrompt.hidden = YES;
            }
            
            for (int i = 0; i<self.likeUsers.count; i++) {
                 MLUserLike *userLike = [self.likeUsers objectAtIndex:i];
                [Utils savePassLoveUserObjectID:userLike.likeUser.objectId];
                
                for (int j = i+1; j<self.likeUsers.count; j++) {
                    MLUserLike *nUserLike = [self.likeUsers objectAtIndex:j];
                    if ([userLike.likeUser.objectId isEqualToString:nUserLike.likeUser.objectId]) {
                        [self.likeUsers removeObjectAtIndex:j];
                        [nUserLike deleteInBackground];
                        j--;
                    }
                }
            }
            [self.tableView reloadData];
            self.currentPage++;
        }
    }];
}



#pragma mark - tableview delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *CellIdentifier = @"FriendApplyCell";
    UserCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if  (cell == nil) {
        cell = [[UserCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    
    MLUserLike *userLike = self.likeUsers[indexPath.row];
    MLUser *applier = (MLUser *)userLike.likeUser;
    
    cell.userNameLabel.text = applier.nickname;
    if (!applier.subAvatar || [applier.subAvatar isEqualToString:@""]) {
        [cell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:applier.avatar.url] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
        
    }else{
        [cell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:applier.subAvatar] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
    }
    //[cell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:applier.avatar.url] placeholderImage:[UIImage imageWithColor:[UIColor whiteColor]]];
    [cell.userAgeLabel setTitle:[NSString stringWithFormat:@"%ld", (long)[applier getUserAge]] forState:UIControlStateNormal];
    if (applier.gender == MLUserGender_MALE) {
        [cell.userAgeLabel setBackgroundImage:[UIImage imageNamed:@"boy_age"] forState:UIControlStateNormal];
    } else {
        [cell.userAgeLabel setBackgroundImage:[UIImage imageNamed:@"girl_age"] forState:UIControlStateNormal];
    }
    
    [cell.userSeriesLabel setBackgroundImage:[UIImage imageNamed:applier.belong] forState:UIControlStateNormal];
    cell.userSeriesLabel.selected = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return  cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MLUserLike *userLike = self.likeUsers[indexPath.row];
    MLUser *likeUser = (MLUser *)userLike.likeUser;
    
    // 跳转到个人资料页
    MLUserDataViewController *userInfoVC = [[MLUserDataViewController alloc]init];
    userInfoVC.userData = likeUser;
    // userInfoVC.likeUsers = self.likeUsers;
    userInfoVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:userInfoVC animated:YES];

}

//cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.likeUsers count];//Cell个数
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

@end
